<?php
require_once 'config.php';

// 获取图书ID
$book_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($book_id <= 0) {
    $_SESSION['flash_message'] = "无效的图书ID";
    $_SESSION['flash_type'] = "danger";
    redirect('books.php');
}

try {
    // 获取图书详情
    $stmt = $pdo->prepare("SELECT * FROM books WHERE book_id = ?");
    $stmt->execute([$book_id]);
    $book = $stmt->fetch();
    
    if (!$book) {
        $_SESSION['flash_message'] = "未找到该图书";
        $_SESSION['flash_type'] = "danger";
        redirect('books.php');
    }
} catch(PDOException $e) {
    $_SESSION['flash_message'] = "获取图书信息失败: " . $e->getMessage();
    $_SESSION['flash_type'] = "danger";
    redirect('books.php');
}

// 页面标题
$page_title = htmlspecialchars($book['title']) . " - 图书详情";

// 引入头部
include 'header.php';
?>

<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="index.php">首页</a></li>
        <li class="breadcrumb-item"><a href="books.php">图书列表</a></li>
        <li class="breadcrumb-item active" aria-current="page"><?php echo htmlspecialchars($book['title']); ?></li>
    </ol>
</nav>

<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h4><?php echo htmlspecialchars($book['title']); ?></h4>
            </div>
            <div class="card-body">
                <h5 class="card-title">作者: <?php echo htmlspecialchars($book['author']); ?></h5>
                <p class="card-text">
                    <strong>出版社:</strong> <?php echo htmlspecialchars($book['publisher']); ?><br>
                    <strong>出版年份:</strong> <?php echo htmlspecialchars($book['publication_year']); ?><br>
                    <strong>ISBN:</strong> <?php echo htmlspecialchars($book['isbn']); ?><br>
                    <strong>分类:</strong> <?php echo htmlspecialchars($book['category']); ?><br>
                    <strong>库存数量:</strong> <?php echo htmlspecialchars($book['quantity']); ?><br>
                    <strong>可借数量:</strong> <?php echo htmlspecialchars($book['available']); ?>
                </p>
                
                <h5 class="mt-4">图书简介</h5>
                <p class="card-text"><?php echo nl2br(htmlspecialchars($book['description'])); ?></p>
                
                <div class="mt-4">
                    <?php if ($book['available'] > 0): ?>
                        <span class="badge badge-success">可借阅</span>
                        <?php if (isLoggedIn()): ?>
                            <a href="borrow.php?id=<?php echo $book['book_id']; ?>" class="btn btn-primary ml-2">借阅此书</a>
                        <?php else: ?>
                            <a href="login.php" class="btn btn-outline-primary ml-2">登录后借阅</a>
                        <?php endif; ?>
                    <?php else: ?>
                        <span class="badge badge-secondary">已借完</span>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h5>同类图书推荐</h5>
            </div>
            <div class="card-body">
                <?php
                try {
                    // 获取同类图书
                    $stmt = $pdo->prepare("SELECT * FROM books WHERE category = ? AND book_id != ? ORDER BY RAND() LIMIT 3");
                    $stmt->execute([$book['category'], $book_id]);
                    $related_books = $stmt->fetchAll();
                    
                    if (empty($related_books)):
                ?>
                    <p class="card-text">暂无相关推荐</p>
                <?php else: ?>
                    <ul class="list-group list-group-flush">
                        <?php foreach ($related_books as $related_book): ?>
                            <li class="list-group-item">
                                <a href="book_detail.php?id=<?php echo $related_book['book_id']; ?>">
                                    <?php echo htmlspecialchars($related_book['title']); ?>
                                </a>
                                <br>
                                <small class="text-muted"><?php echo htmlspecialchars($related_book['author']); ?></small>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php 
                    endif;
                } catch(PDOException $e) {
                    echo '<p class="card-text">获取推荐失败</p>';
                }
                ?>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?> 